package com.renwei.yunlong.activity;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.pager.MeUpcomingAllPager;
import com.renwei.yunlong.pager.MeUpcomingComplaintsPager;
import com.renwei.yunlong.pager.MeUpcomingProblemPager;
import com.renwei.yunlong.pager.MeUpcomingSparePager;
import com.renwei.yunlong.pager.MeUpcomingWorkPager;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的待办--页面
 * <p>
 * Created by Administrator on 2017/9/6.
 */

public class MeUpcomingActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tab_layout)
    TabLayout mTabLayout;
    @Bind(R.id.v_pager)
    ViewPager mViewPager;
    @Bind(R.id.iv_filter)
    ImageView ivFilter;

    private MeUpcomingAdapter adapter;
    private PopupWindow popupWindow;
    RelativeLayout rlSelectOwner;

    private List<Fragment> list;
    private String[] titles = {"全部", "工单", "问题", "投诉", "备件"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_me_upcoming);
        ButterKnife.bind(this);

        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);
        initData();

    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);

        ivFilter.setOnClickListener(this);

        String upcoming = getIntent().getStringExtra("upcoming");
        String handled = getIntent().getStringExtra("handled");
        if ("upcoming".equals(upcoming)) {
            tvTitle.setText("我的待办");
        } else if ("handled".equals(handled)) {
            tvTitle.setText("我的经办");
        }

        //页面，数据源
        list = new ArrayList<>();
        list.add(new MeUpcomingAllPager());//全部
        list.add(new MeUpcomingWorkPager());//工单
        list.add(new MeUpcomingProblemPager());//问题
        list.add(new MeUpcomingComplaintsPager());//投诉
        list.add(new MeUpcomingSparePager());//备件
        //ViewPager的适配器
        adapter = new MeUpcomingAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        //绑定
        mTabLayout.setupWithViewPager(mViewPager);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_filter:
                showPopuWindow(v);
                break;
            default:
                break;
        }
    }

    //popupWindow弹框
    private void showPopuWindowOwnerAndProject(View view) {

        View contentView = LayoutInflater.from(this).inflate(R.layout.popu_filter_layout, null);
        AutoUtils.auto(contentView);
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setTouchable(true);
        popupWindow.showAtLocation(view, Gravity.RIGHT, 0, 0);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
        backgroundAlpha(this, 0.5f);//0.0-1.0
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                backgroundAlpha(MeUpcomingActivity.this, 1f);
            }
        });

        ColorDrawable dw = new ColorDrawable(0xb0808080);
        popupWindow.setBackgroundDrawable(dw);
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
    }



    //popupWindow弹框
    private void showPopuWindow(View view) {
        View contentView = LayoutInflater.from(this).inflate(R.layout.popu_filter_layout, null);
        AutoUtils.auto(contentView);
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setTouchable(true);
        popupWindow.showAtLocation(view, Gravity.RIGHT, 0, 0);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        rlSelectOwner = (RelativeLayout) contentView.findViewById(R.id.rl_select_owner);
        rlSelectOwner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //showPopuWindowOwnerAndProject(v);
            }
        });


        backgroundAlpha(this, 0.5f);//0.0-1.0
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                backgroundAlpha(MeUpcomingActivity.this, 1f);
            }
        });

        ColorDrawable dw = new ColorDrawable(0xb0808080);
        popupWindow.setBackgroundDrawable(dw);
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {

        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    class MeUpcomingAdapter extends FragmentStatePagerAdapter {

        public MeUpcomingAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        //重写这个方法，将设置每个Tab的标题
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

}
